package com.ymatou.iapi.shoppingcartservice.testcase;

import java.util.ArrayList;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.shoppingcartservice.parameter.ShoppingcartProductDeleteByIdsReq;
import com.ymatou.iapi.shoppingcartservice.service.ShoppingcartProductDeleteByIdsCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.BuyerShoppingCartService;
import com.ymttest.database.mongo.YmtShoppingCartWapper;
import com.ymttest.utils.EnvSetup;

public class Ts_ShoppingcartProductDeleteByIds {
	
	private static ShoppingcartProductDeleteByIdsCall shoppingcartProductDeleteByIdsCall = new ShoppingcartProductDeleteByIdsCall();
	private static Integer userId = Integer.valueOf(EnvSetup.getData("shoppingcartUserId"));
	private static BuyerShoppingCartService buyerShoppingCartService = new BuyerShoppingCartService();

	YmtShoppingCartWapper ymtShoppingCartWapper = new YmtShoppingCartWapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据shoppingcartId删除购物车商品");
	}
	
	@Before
	public void caseUp() {
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(shoppingcartProductDeleteByIdsCall.getOpurl());
	}	
	
	@After
	public void caseDown() {
		Logger.end();
		try {
			ymtShoppingCartWapper.deleteByUserId(userId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductDeleteByIds_001() throws Exception {
		Logger.start(true,"根据shoppingcartId删除单个购物车商品");
		try{
			Map<String,Object> shoppingCartDB = buyerShoppingCartService.getShoppingCartProduct(userId);
			ShoppingcartProductDeleteByIdsReq req = new ShoppingcartProductDeleteByIdsReq();
			
			String shoppingCartId = String.valueOf(shoppingCartDB.get("_id"));
			String mainOrderId = String.valueOf(shoppingCartDB.get("mainorderid"));
			int status = Integer.parseInt(String.valueOf(shoppingCartDB.get("status")));
			
			ArrayList<String> shoppingCartIds = new ArrayList<String>();
			shoppingCartIds.add(shoppingCartId);
			
			req.setShoppingCartIds(shoppingCartIds);
			req.setMainOrderId(mainOrderId);
			req.setStatus(status);
			req.setUserId(userId);
			
			JSONObject ret = shoppingcartProductDeleteByIdsCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			int count = ymtShoppingCartWapper.getCountByUserId(userId);
			
			Logger.verifyEquals(200, code, "接口code返回异常");
			Logger.verifyEquals(0, count, "mongo数据删除失败");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductDeleteByIds_002() throws Exception {
		Logger.start(true,"根据shoppingcartId批量删除购物车商品");
		try{
			String shoppingCartId1 = buyerShoppingCartService.AddShoppingCartProduct(userId, "catalogId1");
			String shoppingCartId2 = buyerShoppingCartService.AddShoppingCartProduct(userId, "catalogId2");
			ShoppingcartProductDeleteByIdsReq req = new ShoppingcartProductDeleteByIdsReq();
			
			ArrayList<String> shoppingCartIds = new ArrayList<String>();
			shoppingCartIds.add(shoppingCartId1);
			shoppingCartIds.add(shoppingCartId2);
			
			req.setShoppingCartIds(shoppingCartIds);
			req.setUserId(userId);
			
			JSONObject ret = shoppingcartProductDeleteByIdsCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			int count = ymtShoppingCartWapper.getCountByUserId(userId);
			
			Logger.verifyEquals(200, code, "接口code返回异常");
			Logger.verifyEquals(0, count, "mongo数据删除失败");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductDeleteByIds_003() throws Exception {
		Logger.start(true,"输入错误的shoppingcartId删除购物车商品");
		try{
			Map<String,Object> shoppingCartDB = buyerShoppingCartService.getShoppingCartProduct(userId);
			ShoppingcartProductDeleteByIdsReq req = new ShoppingcartProductDeleteByIdsReq();
			
			String mainOrderId = String.valueOf(shoppingCartDB.get("mainorderid"));
			int status = Integer.parseInt(String.valueOf(shoppingCartDB.get("status")));
			
			ArrayList<String> shoppingCartIds = new ArrayList<String>();
			shoppingCartIds.add("asdfas");
			
			req.setShoppingCartIds(shoppingCartIds);
			req.setMainOrderId(mainOrderId);
			req.setStatus(status);
			req.setUserId(userId);
			
			JSONObject ret = shoppingcartProductDeleteByIdsCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			int count = ymtShoppingCartWapper.getCountByUserId(userId);
			
			Logger.verifyEquals(200, code, "接口code返回异常");
			Logger.verifyEquals(1, count, "mongo数据删除失败");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductDeleteByIds_004() throws Exception {
		Logger.start(true,"输入错误的userid删除购物车商品");
		try{
			Map<String,Object> shoppingCartDB = buyerShoppingCartService.getShoppingCartProduct(userId);
			ShoppingcartProductDeleteByIdsReq req = new ShoppingcartProductDeleteByIdsReq();
			
			String shoppingCartId = String.valueOf(shoppingCartDB.get("_id"));
			String mainOrderId = String.valueOf(shoppingCartDB.get("mainorderid"));
			int status = Integer.parseInt(String.valueOf(shoppingCartDB.get("status")));
			
			ArrayList<String> shoppingCartIds = new ArrayList<String>();
			shoppingCartIds.add(shoppingCartId);
			
			req.setShoppingCartIds(shoppingCartIds);
			req.setMainOrderId(mainOrderId);
			req.setStatus(status);
			req.setUserId(1);
			
			JSONObject ret = shoppingcartProductDeleteByIdsCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			int count = ymtShoppingCartWapper.getCountByUserId(userId);
			
			Logger.verifyEquals(200, code, "接口code返回异常");
			Logger.verifyEquals(1, count, "mongo数据删除失败");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductDeleteByIds_005() throws Exception {
		Logger.start(true,"删除商品未传入userid");
		try{
			Map<String,Object> shoppingCartDB = buyerShoppingCartService.getShoppingCartProduct(userId);
			ShoppingcartProductDeleteByIdsReq req = new ShoppingcartProductDeleteByIdsReq();
			
			String shoppingCartId = String.valueOf(shoppingCartDB.get("_id"));
			String mainOrderId = String.valueOf(shoppingCartDB.get("mainorderid"));
			int status = Integer.parseInt(String.valueOf(shoppingCartDB.get("status")));
			
			ArrayList<String> shoppingCartIds = new ArrayList<String>();
			shoppingCartIds.add(shoppingCartId);
			
			req.setShoppingCartIds(shoppingCartIds);
			req.setMainOrderId(mainOrderId);
			req.setStatus(status);
			req.setUserId(0);
			
			JSONObject ret = shoppingcartProductDeleteByIdsCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			String message = ret.getString("message");
			
			Logger.verifyEquals(500, code, "接口code返回异常");
			Logger.verifyEquals("userId:must be greater than or equal to 1;", message, "未传入userid提示错误");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductDeleteByIds_006() throws Exception {
		Logger.start(true,"删除商品未传入shoppingcartId");
		try{
			ShoppingcartProductDeleteByIdsReq req = new ShoppingcartProductDeleteByIdsReq();
			ArrayList<String> shoppingCartIds = new ArrayList<String>();
			
			req.setShoppingCartIds(shoppingCartIds);
			req.setUserId(userId);
			
			JSONObject ret = shoppingcartProductDeleteByIdsCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			String message = ret.getString("message");
			
			Logger.verifyEquals(500, code, "接口code返回异常");
			Logger.verifyEquals("shoppingCartIds:may not be empty;", message, "未传入shoppingcartId提示错误");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
}
